<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="库房编号" prop="roomId">
						<el-select v-model="form.roomId" clearable filterable remote :remote-method="remoteMethod" style="width: 100%" placeholder="输入库房编号或地址查询">
							<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="检查人" prop="checkUser">
						<select-user-dialog v-model:accountId="form.checkUser" :mode="false"/>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="库房温度" prop="temperature">
						<el-input-number v-model="form.temperature" clearable min="0"></el-input-number>℃
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="库房湿度" prop="humidity">
						<el-input-number v-model="form.humidity" clearable min="0"></el-input-number>%RH
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="检查类型" prop="checkType">
						<sc-select v-model="form.checkType" :params="{code:'checkType'}" :apiObj="$API.archives.archivesDic.getArchivesDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="检查时间" prop="checkTime">
						<el-date-picker v-model="form.checkTime" placeholder="检查时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="备注说明" prop="remark">
				<el-input type="textarea" v-model="form.remark" placeholder="备注说明" maxlength="380" clearable></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">确 认</el-button>
		</template>
	</el-dialog>

</template>
<script>
import AttachMenu from "@/views/module/attachMenu.vue";
import selectUserDialog from "@/views/module/selectUserDialog.vue";
export default {
	emits: ['success', 'closed'],
	components: {
		selectUserDialog,
		AttachMenu,
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			mode: "add",
			titleMap: {
				add: '温湿度管理',
				edit: '温湿度管理',
			},
			form: {
				recordId:'',
				temperature: '',
				humidity: '',
				roomId: '',
				checkTime: 0,
				checkType:'',
				checkUser:'',
				remark:'',
			},
			options: [],
			rules: {
				roomId: [
					{required: true, message: '库房编号不能为空', trigger: 'blur'}
				],
				checkUser:[
					{required: true, message: '检查人不能为空', trigger: 'blur'}
				],
				temperature:[
					{required: true, message: '库房温度不能为空', trigger: 'blur'}
				],
				humidity:[
					{required: true, message: '库房湿度不能为空', trigger: 'blur'}
				],
				checkType:[
					{required: true, message: '检查类型不能为空', trigger: 'blur'}
				],
				checkTime:[
					{required: true, message: '检查时间不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
		this.remoteMethod("");
	},
	methods: {
		async remoteMethod(query){
			let res = await this.$API.archives.archivesRoom.getArchivesRoomListForSelect.get({keyword:query});
			if (res.code == 200) {
				this.options = res.data
			}
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.archives.archivesTempHum.insertArchivesTempHum.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.archives.archivesTempHum.updateArchivesTempHum.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.recordId = data.recordId
			this.form.temperature = data.temperature
			this.form.humidity = data.humidity
			this.form.roomId = data.roomId
			this.form.checkTime = data.checkTime
			this.form.checkType = data.checkType
			this.form.checkUser = data.checkUser
			this.form.remark = data.remark
		},
		addNewFile(res, file)
		{
			let tArr = this.form.attachId.split(",");
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		}
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
